const Redis = require("ioredis");
const redis = new Redis(6379, "127.0.0.1");
const Redlock = require('redlock');
const cluster = require('cluster');
const redlock = new Redlock([redis], {
    retryDelay: 200, // 查询间隔时间
    retryCount: 0,//查询次数
    retryJitter:  200, //添加到重试任务的延迟时间
    driftFactor:0.01
});

const poolKey = 'task:pool'
// 多个 Redis 实例
// const redlock = new Redlock(
//     [new Redis(6379, "127.0.0.1"), new Redis(6379, "127.0.0.2"), new Redis(6379, "127.0.0.3")],
// )

async function test(key, ttl, client) {
    try {
        const lock = await redlock.lock(key, ttl);

        console.log(client, lock.value);
        // do something ...

        // return lock.unlock();

       
    } catch(err) {
        console.error(client, err);
    }
}
/* 
test('name1', 1000, 'client1');
test('name1', 1000, 'client2'); */
let  key = 'task:pool-value'
/* redis.zadd(key,0,3).then(res=>{
    console.log(res)
}) */
// 获取CPU 的数量
/* var numCPUs = 0;
if (cluster.isMaster) {
// 初始开启与CPU 数量相同的工作进程
    for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
    }
} else {
    redis.zrangebyscore(key, '-inf', '+inf', 'WITHSCORES', 'LIMIT', 0, 1).then((res)=>{
        console.log(res)
    }) 
    
    async function aaa(){
        const result3 = redis.eval(`return redis.call('ZRANGEBYSCORE',  KEYS[1], ARGV[1],ARGV[2], ARGV[3],ARGV[4], ARGV[5], ARGV[6])`,1,key,'-100', '(100', 'WITHSCORES','LIMIT',0,1);
        let [keys,score] = await result3;
        let ddd = null;
        if(keys){
            const result2 = redis.eval(`return redis.call('ZINCRBY',  KEYS[1], ARGV[1],ARGV[2])`,1,key,Date.now(),keys);
             ddd = await result2;
        }
        
        console.log(keys,score,ddd)
    }
    // aaa()
   
}  */
console.log('fgggg')
redis.zrangebyscore(key, '0', '9999999999999999999999999', 'WITHSCORES', 'LIMIT',0,10000000000000).then((res)=>{
    console.log(res.length)
})
async function bbbb(){
    let i  = 0
    while(i<1000000){
        await new Promise((resolve,reject)=>{
            setTimeout(()=>{
                resolve()
            },10)
        })
        redis.zadd(key,0,JSON.stringify({
            "type": "shopNewDetail",
            "isSuc": true,
            "errInfo": null,
            "msg_data": {
                "task_id": 2586,
                "sub_task_id": "344944141",
                "task_key": "2586-344944141-NEW_ARRIVAL_CYCLE",
                "notify_key": "2586-344944141-NEW_ARRIVAL_CYCLE",
                "publish_time": 1629045624,
                "item_id": 653525635512,
                "node_cycle": 4,
                "node_time": 1630772771
            },
            "timeStamp":Date.now()
        }))
    }
}

// bbbb()
